package frameData;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import message.Message;

import data.mainFrame;
import data.subFrame;
import factory.CourseFactory;
import frameDataService.FrameDataService;

import PO.FramePO;

public class FrameDataController extends UnicastRemoteObject implements FrameDataService {


	FrameData fd;
	CourseFactory cf;
	public FrameDataController() throws RemoteException {
		// TODO Auto-generated constructor stub
		fd = new FrameData();
		cf = new CourseFactory();
	}


	@Override
	public Message add(FramePO framePO) {
		// TODO Auto-generated method stub
		mainFrame mf = cf.mainFrameMake(framePO);
		subFrame[] sf = cf.subFrameMake(framePO);
		
		return fd.addFrame(mf, sf);
		
	}


	@Override
	public Message delete(FramePO framePO) {
		// TODO Auto-generated method stub
		mainFrame mf = cf.mainFrameMake(framePO);
		return fd.deleteFrame(mf);
	}

	public ArrayList<FramePO> find(){
		ArrayList<FramePO> result = new ArrayList<FramePO>();

		ArrayList<mainFrame> list = fd.findMainFrame();
		for(int i = 0;i<list.size();i++){
			mainFrame mf = list.get(i);
			String serialNumber = mf.getSerialNumber();
			subFrame[] sb = fd.findSubFraem(serialNumber);				
			result.add(cf.FramePOMake(mf, sb));
		}
		
		return result;
	}
	@Override
	public Message update(FramePO old, FramePO newOne) throws RemoteException {
		// TODO Auto-generated method stub
		CourseFactory cf = new CourseFactory();
		mainFrame mf = cf.mainFrameMake(newOne);
		subFrame[] sf = cf.subFrameMake(newOne);
		
		mainFrame om = cf.mainFrameMake(old);
		subFrame[] os = cf.subFrameMake(old);
		
		return fd.update(om, os, mf, sf);
	}
	@Override
	public Message submit(ArrayList<String> plan, String dp) throws RemoteException {
		// TODO Auto-generated method stub
		
		return fd.submit(plan, dp);
	}
	@Override
	public ArrayList<String> downLoad(String dp) throws RemoteException {
		// TODO Auto-generated method stub
		
		return fd.downLoad(dp);
	}

}
